package cn.sumpu.app.guoguojie.biz.impl;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

import magick.ImageInfo;
import magick.MagickException;
import magick.MagickImage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import cn.sumpu.app.guoguojie.biz.ImageBiz;
import cn.sumpu.app.guoguojie.dao.DoingDao;
import cn.sumpu.app.guoguojie.domain.DoingPicture;
import cn.sumpu.app.guoguojie.domain.DoingProduct;
import cn.sumpu.app.guoguojie.domain.DoingSync;
import cn.sumpu.app.guoguojie.domain.Member;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.Common;
import cn.sumpu.app.guoguojie.util.Constant;
import cn.sumpu.app.guoguojie.util.HttpProxy;
import cn.sumpu.app.guoguojie.util.ImageUtil;
import cn.sumpu.app.guoguojie.util.ImageWeb;

public class ImageBizImpl implements ImageBiz {

    private final static Logger logger = LoggerFactory.getLogger(ImageBizImpl.class);

    static {
        System.setProperty("jmagick.systemclassloader", "no");
    }

    private String              uploadPath;
    private String              imageServer;
    private String              imageDir;

    private DoingDao            doingDao;

    public void setDoingDao(DoingDao doingDao) {
        this.doingDao = doingDao;
    }

    public void setUploadPath(String uploadPath) {
        this.uploadPath = uploadPath;
    }

    public String getImageServer() {
        return imageServer;
    }

    public void setImageServer(String imageServer) {
        this.imageServer = imageServer;
    }

    public void setImageDir(String imageDir) {
        this.imageDir = imageDir;
    }

    /**
     * @param srcImgPath
     *            要被切割的图像地址
     * @param descDir
     *            要保存图像的地址,如果是保存到服务器则是服务器的地址
     * @param imageName
     *            保存图片的名称
     * @param x
     *            要切割的图片的在原图片的x坐标
     * @param y
     *            要切割的图片的在原图片的y坐标
     * @param destWidth
     *            要切割图片的宽度
     * @param destHeight
     *            要切割图片的高度
     * @param divWidth
     *            要切割图片的分辨率的宽度
     * @param divHeight
     *            客户端原图片外层的高度
     * @param tagWidth
     *            要切割图片的分辨率的宽度
     * @param tagHeight
     *            要切割图片的分表率的高度
     * @return
     * @throws AppBusinessException
     */
    @Override
    public ImageWeb cutAvatar(String readImageServer, String srcImgPath, String descDir, String imageName, int x, int y, int destWidth, int destHeight,
                              int divWidth, int divHeight, int tagWidth, int tagHeight, String photoType) throws AppBusinessException {
        try {

            Image img;
            ImageFilter cropFilter;
            BufferedImage bi = null;
            if (photoType.equals("avatar")) {
                bi = ImageIO.read(new File(srcImgPath));
            } else if (Integer.parseInt(photoType) == 2) {
                URL url = new URL(readImageServer + srcImgPath);
                bi = ImageIO.read(url.openStream());
            }
            int srcWidth = bi.getWidth();
            int srcHeight = bi.getHeight();

            double zoom = (double) srcWidth / divWidth;
            double zoomHeight = (double) srcHeight / divHeight;
            if (zoomHeight > zoom) {
                zoom = zoomHeight;
            }
            if (destWidth == 0 || destHeight == 0) {
                destWidth = divWidth;
                destHeight = divHeight;
            }
            int tmpTagWidth = (int) (destWidth * zoom);
            int tmpTagHeight = (int) (destHeight * zoom);
            if (tmpTagWidth < tagWidth && tmpTagHeight < tagHeight) {
                tagWidth = tmpTagWidth;
                tagHeight = tmpTagHeight;
            }
            cropFilter = new CropImageFilter((int) (x * zoom), (int) (y * zoom), (int) (destWidth * zoom), (int) (destHeight * zoom));
            img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(bi.getSource(), cropFilter));

            BufferedImage tag = new BufferedImage(tagWidth, tagWidth, BufferedImage.TYPE_INT_RGB);
            Graphics g = tag.getGraphics();
            g.drawImage(img, 0, 0, tagWidth, tagWidth, null);
            g.dispose();

            String serverUrl = imageServer + "receiveImage.htm";
            ByteArrayOutputStream bao_org = new ByteArrayOutputStream();
            ImageIO.write(tag, "jpg", bao_org);
            String imgDirPath = uploadPath + "/" + photoType;
            String imgName = imageName + ".jpg";
            requestSaveImage(serverUrl, imgDirPath, imgName, bao_org.toByteArray());
            ImageWeb imageWeb = new ImageWeb();
            imageWeb.setImageServer(imageServer);
            imageWeb.setImageUrl(imgDirPath + "/" + imgName);
            return imageWeb;

        } catch (Exception e) {
            e.printStackTrace();
            throw new AppBusinessException("切割头像失败", e);
        }
    }

    public ImageWeb cutImage(byte[] imageByte, int tagWidth, int tagHeight, String savePath, String imageName) throws AppBusinessException {
        ImageWeb iw = new ImageWeb();
        try {
            String serverUrl = imageServer + "receiveImage.htm";
            ImageInfo info = new ImageInfo();
            MagickImage image = new MagickImage(info, imageByte);
            Dimension imageDim = image.getDimension();
            int srcWidth = imageDim.width;
            int srcHeight = imageDim.height;
            double zoom = 1;
            double zoomHeight = 1;
            if (tagWidth == -1) {
                zoom = (double) tagHeight / srcHeight;
                tagWidth = (int) (srcWidth * zoom);
            } else {
                zoom = (double) tagWidth / srcWidth;
                if (tagHeight == -1) {
                    tagHeight = (int) (srcHeight * zoom);
                } else {
                    zoomHeight = (double) tagHeight / srcHeight;
                }
            }

            if (zoom < 1 || zoomHeight < 1) {
                zoomHeight = (double) tagHeight / srcHeight;
                Rectangle rect = new Rectangle(0, 0, tagWidth, tagHeight);
                MagickImage targetImg = null;
                MagickImage scaleImg = null;
                if (zoom > zoomHeight) {
                    scaleImg = image.scaleImage(tagWidth, (int) (srcHeight * zoom));
                } else {
                    scaleImg = image.scaleImage((int) (srcWidth * zoomHeight), (int) (srcHeight * zoomHeight));
                }
                scaleImg.setImageFormat("JPG");
                targetImg = scaleImg.cropImage(rect);
                requestSaveImage(serverUrl, savePath, imageName, targetImg.imageToBlob(info));
                iw.setWidth(tagWidth);
                iw.setHeight(tagHeight);
            } else {
                requestSaveImage(serverUrl, savePath, imageName, imageByte);
                iw.setWidth(srcWidth);
                iw.setHeight(srcHeight);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new AppBusinessException("cut image error!", e);
        }
        return iw;
    }

    @Override
    public String makeThumbnail(String srcImgPath, Map<String, Object> settings) throws AppBusinessException {
        return ImageUtil.makeThumbnail(srcImgPath, settings);
    }

    @Override
    public void makeWatermark(String waterText, String targetImg, Font font, Color color, int watermarkPos) throws AppBusinessException {
        ImageUtil.makeWatermark(waterText, targetImg, font, color, watermarkPos);
    }

    @Override
    public void makeWatermark(String waterImg, String targetImg, int watermarkPos) throws AppBusinessException {
        ImageUtil.makeWatermark(waterImg, targetImg, watermarkPos);
    }

    @Override
    public String makeThumbnail(String srcImgPath, int thumbnailWidth, int thumbnailHeight, int maxThumbnailWidth, int maxThumbnailHeight)
                                                                                                                                          throws AppBusinessException {
        return ImageUtil.makeThumbnail(srcImgPath, thumbnailWidth, thumbnailHeight, maxThumbnailWidth, maxThumbnailHeight);
    }

    @Override
    public boolean checkUploadSize(HttpServletRequest request) throws AppBusinessException {
        try {
            MultipartHttpServletRequest re = (MultipartHttpServletRequest) request;
            Map<String, MultipartFile> map = re.getFileMap();
            List<MultipartFile> fileList = new ArrayList<MultipartFile>();
            for (Object obj : map.values()) {
                MultipartFile file = (MultipartFile) obj;
                fileList.add(file);
            }
            if (fileList.size() > 0) {
                MultipartFile fj = fileList.get(0);
                if (fj.getSize() > Common.PHOTO_SIZE_ALLOW) {
                    return false;
                } else
                    return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            throw new AppBusinessException("check upplad size failure!", e);
        }

    }

    @Override
    public void upload(HttpServletRequest request) throws AppBusinessException {
        MultipartHttpServletRequest re = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> map = re.getFileMap();
        List<MultipartFile> fileList = new ArrayList<MultipartFile>();
        for (Object obj : map.values()) {
            MultipartFile file = (MultipartFile) obj;
            fileList.add(file);
        }
        if (fileList.size() > 0) {
            String webAppPath = request.getSession().getServletContext().getRealPath("/");
            Member member = (Member) request.getSession().getAttribute("member");
            String family = member.getFamily();
            String formateDate = Common.getFormatDate(new Date());
            String tmpPath = webAppPath + File.separator + "tmp" + File.separator + "pic" + File.separator + formateDate + File.separator + family;
            File uploadfujian = new File(tmpPath);
            if (!uploadfujian.exists()) {
                uploadfujian.mkdirs();
            }
            for (int i = 0; i < fileList.size(); i++) {
                MultipartFile fj = fileList.get(i);
                String fn = fj.getOriginalFilename();
                if (null != fn && fn.length() > 0) {
                    String uuid = UUID.randomUUID().toString();
                    String fujianpath = uploadfujian + File.separator + uuid + ".jpg";
                    try {
                        InputStream buffin = new ByteArrayInputStream(fj.getBytes());
                        BufferedImage bi = ImageIO.read(buffin);

                        ImageIO.write(bi, "JPG", new File(fujianpath));

                    } catch (IOException e) {
                        e.printStackTrace();
                        throw new AppBusinessException("upload fail", e);
                    }
                }
            }
        }
    }

    @Override
    public ImageWeb uploadOneImg(HttpServletRequest request) throws AppBusinessException {
        ImageWeb imageWeb = new ImageWeb();
        try {
            MultipartHttpServletRequest re = (MultipartHttpServletRequest) request;
            Map<String, MultipartFile> map = re.getFileMap();
            List<MultipartFile> fileList = new ArrayList<MultipartFile>();
            for (Object obj : map.values()) {
                MultipartFile file = (MultipartFile) obj;
                fileList.add(file);
            }
            if (fileList.size() > 0) {
                MultipartFile file = fileList.get(0);
                String originalName = file.getOriginalFilename();
                if (null != originalName && originalName.length() > 0) {
                    if (file.getSize() > Common.PHOTO_SIZE_ALLOW) {
                        imageWeb.setSizeAllow(false);
                        return imageWeb;
                    }
                    InputStream buffin = new ByteArrayInputStream(file.getBytes());
                    BufferedImage bi = ImageIO.read(buffin);
                    if (bi != null) { 
                        String fileType = (String) request.getAttribute("fileType");
                        String fileName = (String) request.getAttribute("fileName");
                        //String imageFix = originalName.substring(originalName.lastIndexOf("."));
                        String imageFix = ".jpg";
                        String relativeDirPath = uploadPath + File.separator + fileType;
                        String relativeFilePath = relativeDirPath + File.separator + fileName+imageFix; //图片相对路径
                        String realDirPath = imageDir + File.separator + relativeDirPath;
                        String realFilePath = imageDir +File.separator+ relativeFilePath;//图片真实路径
                        //生成文件夹
                        File uploadfujian = new File(realDirPath);
                        if (!uploadfujian.exists()) {
                            uploadfujian.mkdirs();
                        }
                        FileOutputStream output = new FileOutputStream(realFilePath);
                        FileCopyUtils.copy(file.getBytes(), output);
                        output.close();
                        String imageUrl = relativeFilePath; 
                        imageWeb.setImageUrl(imageUrl);
                        imageWeb.setImageServer(imageServer);
                        imageWeb.setWidth(bi.getWidth());
                        imageWeb.setHeight(bi.getHeight());
                        imageWeb.setSizeAllow(true);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new AppBusinessException("upload one img failure!", e);
        }
        return imageWeb;
    }

    @Override
    public void requestSaveImage(String serverUrl, String savePath, String saveFilename, byte[] imageData) throws AppBusinessException {
        try {
            String ret = HttpProxy.sendImage(serverUrl, savePath, saveFilename, imageData);
            if (null == ret || "error".equals(ret)) {
                if (logger.isErrorEnabled()) {
                    logger.error("Request save image failure!");
                }
                throw new AppBusinessException();
            }
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("Request save image failure!", e);
            }
            throw new AppBusinessException(e);
        }
    }

    @Override
    public void requestDeleteImage(String serverUrl, String imageFile) throws AppBusinessException {
        try {
            String ret = HttpProxy.deleteImage(serverUrl, imageFile);
            if (null == ret || "error".equals(ret)) {
                if (logger.isErrorEnabled()) {
                    logger.error("Request delete image failure!");
                }
                throw new AppBusinessException();
            }
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("Request delete image failure!", e);
            }
            throw new AppBusinessException(e);
        }
    }

    @Override
    public void requestDeleteImage(String imageFile) throws AppBusinessException {
        requestDeleteImage(imageServer + "deleteImage.htm", imageFile);
    }

    @Override
    public void saveImage(byte[] in, String savePath, String saveFilename) throws AppBusinessException {
        try {
            String path = imageDir + File.separator + savePath + File.separator;
            File filePath = new File(path);
            if (!filePath.exists()) {
                filePath.mkdirs();
            }
            String fileName = path + saveFilename;
            File f = new File(fileName);
            FileOutputStream out = new FileOutputStream(f);
            out.write(in);
            out.close();
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("Save image failure!", e);
            }
            throw new AppBusinessException(e);
        }
    }

    @Override
    public void deleteImage(String imageFile) throws AppBusinessException {
        try {
            String fileName = imageDir + File.separator + imageFile;
            File file = new File(fileName);
            file.delete();
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("Delete image failure!", e);
            }
            throw new AppBusinessException(e);
        }
    }

    @Override
    public void setWidthHeightPic() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("needHeight", "needHeight");
        List<DoingPicture> dpicList = doingDao.getDoingPictureList(map);
        try {
            for (DoingPicture dpic : dpicList) {
                String picUrl = dpic.getImageServer() + dpic.getPictureThumbnail() + "_200xXXX.jpg";
                byte[] imageByte = Common.picUrlToByte(picUrl);
                if (imageByte != null) {
                    ImageInfo info = new ImageInfo();
                    MagickImage image = new MagickImage(info, imageByte);
                    Dimension imageDim = image.getDimension();
                    int srcWidth = imageDim.width;
                    int srcHeight = imageDim.height;
                    dpic.setWidth200xxx(srcWidth);
                    dpic.setHeight200xxx(srcHeight);
                    doingDao.updateDoingPicture(dpic);
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void cut320Pic() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        List<DoingPicture> dpicList = doingDao.getDoingPictureList(map);

        for (DoingPicture dpic : dpicList) {
            String fileName = imageDir + "/" + dpic.getPicUrl();

            try {
                ImageInfo info = new ImageInfo(fileName);
                MagickImage image = new MagickImage(info);
                Dimension imageDim = image.getDimension();
                int srcWidth = imageDim.width;
                int srcHeight = imageDim.height;
                int targetWidth = srcWidth;
                int targetHeight = srcHeight;
                double zoom = (double) 320 / srcWidth;
                if (zoom < 1) {
                    targetWidth = 320;
                    targetHeight = (int) (zoom * srcHeight);
                }
                MagickImage cropped = image.scaleImage(targetWidth, targetHeight);
                cropped.setFileName(imageDir + "/" + dpic.getPictureThumbnail() + "_320xXXX.jpg");
                cropped.writeImage(info);
                System.out.println("success:" + imageDir + "/" + dpic.getPictureThumbnail() + "_320xXXX.jpg");
            } catch (Exception e) {
                e.printStackTrace();
                logger.error("cut300pic failure---" + fileName, e);
                //	System.out.println("失败：C:/workspaces/guoguojie/guoguojie-web/src/main/webapp/"+dpic.getPictureThumbnail()+"_200xXXX.jpg");
            }
        }

    }

    @Override
    public void cut320Pro() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        List<DoingProduct> dproList = doingDao.getDoingProductList(map);
        for (DoingProduct dpro : dproList) {
            String fileName = imageDir + "/" + dpro.getPicThumb() + "_468xXXX.jpg";

            try {
                ImageInfo info = new ImageInfo(fileName);
                MagickImage image = new MagickImage(info);
                Dimension imageDim = image.getDimension();
                int srcWidth = imageDim.width;
                int srcHeight = imageDim.height;
                int targetWidth = srcWidth;
                int targetHeight = srcHeight;
                double zoom = (double) 320 / srcWidth;
                if (zoom < 1) {
                    targetWidth = 320;
                    targetHeight = (int) (zoom * srcHeight);
                }
                MagickImage cropped = image.scaleImage(targetWidth, targetHeight);
                cropped.setFileName(imageDir + "/" + dpro.getPicThumb() + "_320xXXX.jpg");
                cropped.writeImage(info);
                System.out.println("成功:" + imageDir + "/" + dpro.getPicThumb() + "_320xXXX.jpg");
            } catch (Exception e) {
                //	e.printStackTrace();
                logger.error("cut300pro failure---" + fileName, e);
                System.out.println("失败：" + imageDir + "/" + dpro.getPicThumb() + "_200xXXX.jpg");
            }
        }
    }

    @Override
    public void correctImg() throws Exception {
        String picPath = imageDir + "/pic/doing";
        File file = new File(picPath);
        File[] dateList = file.listFiles();
        for (File dateF : dateList) {
            String date = dateF.getName();
            File[] familyList = dateF.listFiles();
            for (File familyF : familyList) {
                String family = familyF.getName();
                File[] memberIdList = familyF.listFiles();
                for (File memberIdF : memberIdList) {
                    String memberId = memberIdF.getName();
                    File[] imgList = memberIdF.listFiles();
                    for (File img : imgList) {
                        String imgName = img.getName();
                        if (!Common.empty(imgName)) {
                            boolean isThumb = imgName.indexOf("_thumb") != -1;
                            if (isThumb) {
                                imgName = imgName.substring(0, imgName.length() - 12);
                            }
                            String pic = "pic/doing/" + date + "/" + family + "/" + memberId + "/" + imgName;
                            Map<String, Object> map = new HashMap<String, Object>();
                            if (isThumb) {
                                map.put("picThumb", pic);
                                map.put("pictureThumbnail", pic);
                            } else {
                                map.put("picUrl", pic);
                                map.put("pictureUrl", pic);
                            }
                            List<DoingPicture> dpicList = doingDao.getDoingPictureList(map);
                            if (dpicList == null || dpicList.size() < 1) {
                                List<DoingProduct> dproList = doingDao.getDoingProductList(map);
                                if (dproList == null || dproList.size() < 1) {
                                    List<DoingSync> dsList = doingDao.getDoingSync(map);
                                    if (dsList == null || dsList.size() < 1) {
                                        img.delete();
                                        if (logger.isWarnEnabled()) {
                                            logger.warn("delete file success: " + pic);
                                        }
                                    }
                                }

                            }

                        }
                        //	System.out.println("pic/doing/"+date+"/"+family+"/"+memberId+"/"+imgName);
                    }
                }
            }
        }

    }

    @Override
    public void setWidthHeightPro() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("needHeight", "needHeight");
        List<DoingProduct> dproList = doingDao.getDoingProductList(map);
        try {
            for (DoingProduct dpro : dproList) {
                String picUrl = dpro.getImageServer() + dpro.getPicThumb() + "_200xXXX.jpg";
                byte[] imageByte = Common.picUrlToByte(picUrl);
                if (imageByte != null) {
                    ImageInfo info = new ImageInfo();
                    MagickImage image = new MagickImage(info, imageByte);
                    Dimension imageDim = image.getDimension();
                    int srcWidth = imageDim.width;
                    int srcHeight = imageDim.height;
                    // System.out.println("srcWidth:"+srcWidth+" srcHeight:"+srcHeight);
                    dpro.setWidth200xxx(srcWidth);
                    dpro.setHeight200xxx(srcHeight);
                    doingDao.updateDoingProduct(dpro);
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) throws MagickException {
        String fileName = "C:/diany.jpg";
        ImageInfo info = new ImageInfo(fileName);
        MagickImage image = new MagickImage(info);
        Dimension imageDim = image.getDimension();
        int srcWidth = imageDim.width;
        int srcHeight = imageDim.height;
        int targetWidth = srcWidth;
        int targetHeight = srcHeight;
        double zoom = (double) 320 / srcWidth;
        if (zoom < 1) {
            targetWidth = 320;
            targetHeight = (int) (zoom * srcHeight);
        }
        MagickImage cropped = image.scaleImage(targetWidth, targetHeight);
        cropped.setFileName("C:/test.jpg");
        cropped.writeImage(info);
    }

    @Override
    public void requestDeleteImageThumb(String imageFile) throws AppBusinessException {
        requestDeleteImage(imageFile + "_100x100.jpg");
        requestDeleteImage(imageFile + "_150x160.jpg");
        requestDeleteImage(imageFile + "_200x200.jpg");
        requestDeleteImage(imageFile + "_200xXXX.jpg");
        requestDeleteImage(imageFile + "_320xXXX.jpg");
        requestDeleteImage(imageFile + "_468xXXX.jpg");

    }

    @Override
    public ImageWeb uploadOneImg(HttpServletRequest request, String picType) throws AppBusinessException {
        ImageWeb imageWeb = new ImageWeb();
        imageWeb.setState("上传失败！");
        try {
            MultipartHttpServletRequest re = (MultipartHttpServletRequest) request;
            Map<String, MultipartFile> map = re.getFileMap();
            List<MultipartFile> fileList = new ArrayList<MultipartFile>();
            for (Object obj : map.values()) {
                MultipartFile file = (MultipartFile) obj;
                fileList.add(file);
            }
            if (fileList.size() > 0) {

                MultipartFile fj = fileList.get(0);
                String fn = fj.getOriginalFilename();
                if (null != fn && fn.length() > 0) {
                    // 图片不能超过允许的大小
                    if (fj.getSize() > Constant.PHOTO_SIZE_ALLOW) {
                        imageWeb.setSizeAllow(false);
                        return imageWeb;
                    } else {

                        InputStream buffin = new ByteArrayInputStream(fj.getBytes());
                        BufferedImage bi = ImageIO.read(buffin);
                        if (bi != null) {
                            String now = Common.getFormatDate(new Date(), "yyyyMMdd");
                            String uuid = UUID.randomUUID().toString();
                            String imageFix = fn.substring(fn.lastIndexOf("."));

                            String serverUrl = imageServer + "receiveImage.htm";
                            String savePath = uploadPath + "/" + picType + "/" + now;
                            requestSaveImage(serverUrl, savePath, uuid + imageFix, fj.getBytes());
                            imageWeb.setImageServer(imageServer);
                            imageWeb.setImageUrl(uploadPath + "/" + picType + "/" + now + "/" + uuid + imageFix);
                            imageWeb.setWidth(bi.getWidth());
                            imageWeb.setHeight(bi.getHeight());
                            imageWeb.setSizeAllow(true);
                            imageWeb.setState("SUCCESS");
                        }
                        return imageWeb;
                    }

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return imageWeb;
        }
        return imageWeb;
    }

}
